<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Users;
use App\Models\Templatemini;
use App\Models\Templateadmin;
use App\Models\Linkslists;
use App\Models\Miniappsauthorization;
use App\Models\Minappslists;
use App\Models\Miniappsbeian;
use App\Models\Templatebeian;
use App\Services\WechatService;
use App\Services\OutputService;
use App\Services\MiniappopenService;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use Auth,Config,Exception,Log;

class TemplateController extends Controller
{
    public function templateMini(Request $request)
    {
        $users = Auth::user();
        $templateMinis = Templatemini::templateMiniLists($users->id);
        return view('admin.template.mini_list',["templateMinis"=>$templateMinis]);
    }

    public function templateMiniAdd(Request $request)
    {
        return view('admin.template.mini_list_add');
    }

    public function templateMiniAddSubmit(Request $request)
    {
        $users = Auth::user();
        $templateMini = Templatemini::templateMiniByName($users->id,$request->enterprise_name);
        if (!empty($templateMini)) return OutputService::error("企业已绑定,请勿重复绑定!");
        $enterprise_name = str_replace(" ","",$request->enterprise_name);
        $code = str_replace(" ","",$request->code);
        $legal_persona_wechat = str_replace(" ","",$request->legal_persona_wechat);
        $legal_persona_name = str_replace(" ","",$request->legal_persona_name);
        Templatemini::templateMiniInsert($users->id,$enterprise_name,$request->code_type,$code,$legal_persona_wechat,$legal_persona_name);
        return OutputService::success("企业快速注册小程序模版添加成功!");
    }

    public function templateMiniDelete(Request $request)
    {
        $users = Auth::user();
        $templateMini = Templatemini::templateMiniDetial($request->id);
        if (empty($templateMini)) return OutputService::error("企业模板不存在!");
        if ($templateMini->users_id != $users->id) return OutputService::error("企业模板不存在!");
        Templatemini::templateMiniDisable($request->id,2);
        return OutputService::success("企业模板删除成功!");
    }

    public function templateMiniUpdate(Request $request,$id)
    {
        $users = Auth::user();
        $templateMini = Templatemini::templateMiniDetial($id);
        if ($templateMini->users_id != $users->id) return OutputService::error("企业模板不存在!");
        return view('admin.template.mini_list_update',['templateMini'=>$templateMini]);
    }

    public function templateMiniUpdateSubmit(Request $request)
    {
        $users = Auth::user();
        $templateMini = Templatemini::templateMiniDetial($request->id);
        if ($templateMini->users_id != $users->id) return OutputService::error("企业快速注册小程序模板不存在!");
        $enterprise_name = str_replace(" ","",$request->enterprise_name);
        $code = str_replace(" ","",$request->code);
        $legal_persona_wechat = str_replace(" ","",$request->legal_persona_wechat);
        $legal_persona_name = str_replace(" ","",$request->legal_persona_name);
        Templatemini::templateMiniUpdate($request->id,$enterprise_name,$request->code_type,$code,$legal_persona_wechat,$legal_persona_name);
        return OutputService::success("企业快速注册小程序模版更新成功!");
    }

    public function templateBeian(Request $request)
    {
        $users = Auth::user();
        $templateBeians = Templatebeian::templateBeianLists($users->id);
        return view('admin.template.beian_list',["templateBeians"=>$templateBeians]);
    }

    public function templateBeianAdd(Request $request)
    {
        $icp_subject_types = Cache::remember('icp_subject_types', 86400, function () {
            $data = Miniappsauthorization::getOpenTokenDetial('wx8659419830261904');
            Log::info($data);
            $icp_subject_types = MiniappopenService::queryIcpSubjectTypes($data->component_appid,$data->authorizer_appid,$data->authorizer_refresh_token);
            return $icp_subject_types;
        });
        $icp_certificate_types = Cache::remember('icp_certificate_types', 10, function () {
            $data = Miniappsauthorization::getOpenTokenDetial('wx8659419830261904');
            $icp_certificate_types = MiniappopenService::queryIcpCertificateTypes($data->component_appid,$data->authorizer_appid,$data->authorizer_refresh_token);
            return $icp_certificate_types;
        });
        $district = Cache::remember('district_arr', 86400, function () {
            $data = Miniappsauthorization::getOpenTokenDetial('wx8659419830261904');
            $district = MiniappopenService::queryIcpDistrictCode($data->component_appid,$data->authorizer_appid,$data->authorizer_refresh_token);
            return $district;
        });
        return view('admin.template.beian_list_add',['district'=>$district,'icp_subject_types'=>$icp_subject_types,"icp_certificate_types"=>$icp_certificate_types]);
    }

    public function templateBeianAddSubmit(Request $request)
    {
        $users = Auth::user();
        // $beianInfo = json_decode($request->beianInfo,true);
        $altemplate = Templatebeian::templateBeianByName($users->id,$request->certificate_number,$request->principal_info);
        if(!empty($altemplate)) return OutputService::error("企业模版已存在!");
        $beianInfo = $request->beianInfo;
        Templatebeian::templateBeianInsert($users->id,$beianInfo,$request->template_name,$request->province,$request->principal_info,$request->legal_person,$request->certificate_number);
        return OutputService::success("企业模版添加成功!");
    }

    public function templateBeianUpdate(Request $request,$id)
    {
        $users = Auth::user();
        $templateBeian = Templatebeian::templateBeianDetial($id);
        if ($templateBeian->users_id != $users->id) return OutputService::error("企业备案模板不存在!");
        $district = Cache::remember('district_arr', 86400, function () {
            $data = Miniappsauthorization::getOpenTokenDetial('wx8659419830261904');
            $district = MiniappopenService::queryIcpDistrictCode($data->component_appid,$data->authorizer_appid,$data->authorizer_refresh_token);
            return $district;
        });
        return view('admin.template.beian_list_update',['templateBeian'=>$templateBeian,"district"=>$district]);
    }

    public function templateBeianUpdateSubmit(Request $request)
    {
        $users = Auth::user();
        $templateMini = Templatebeian::templateBeianDetial($request->id);
        if ($templateMini->users_id != $users->id) return OutputService::error("企业模板不存在!");
        Templatebeian::templateBeianUpdate($request->id,$request->beianInfo,$request->template_name,$request->province,$request->principal_info,$request->legal_person,$request->certificate_number);
        return OutputService::success("企业模版更新成功!");
    }

    public function templateAdmin(Request $request)
    {
        $users = Auth::user();
        $temAdmins = Templateadmin::templateAdminLists($users->id);
        return view('admin.template.admin_list',["temAdmins"=>$temAdmins]);
    }

    public function templateAdminAdd(Request $request)
    {
        return view('admin.template.admin_list_add');
    }

    public function templateAdminAddSubmit(Request $request)
    {
        $users = Auth::user();
        $temAdmin = Templateadmin::templateAdminByIdcard($users->id,$request->manage_idcard);
        if (!empty($temAdmin)) return OutputService::error("小程序备案管理员已绑定,请勿重复绑定!");
        Templateadmin::templateAdminInsert($users->id,$request->manage_name,$request->manage_idcard,$request->manage_date_start,$request->manage_date_end,$request->manage_email,$request->manage_mobile,$request->manage_emergency_contact,$request->manage_photo_front,$request->manage_photo_back);
        return OutputService::success("小程序备案管理员添加成功!");
    }

    public function templateAdminDelete(Request $request)
    {
        $users = Auth::user();
        $temAdmin = Templateadmin::templateAdminDetial($request->id);
        if (empty($temAdmin)) return OutputService::error("小程序备案管理员模板不存在!");
        if ($temAdmin->users_id != $users->id) return OutputService::error("小程序备案管理员模板不存在!");
        Templateadmin::templateAdminDisable($request->id,2);
        UploadService::deleteOssImage($temAdmin->manage_photo_front);
        UploadService::deleteOssImage($temAdmin->manage_photo_back);
        return OutputService::success("小程序备案管理员模板删除成功!");
    }

    public function templateAdminUpdate(Request $request,$id)
    {
        $users = Auth::user();
        $temAdmin = Templateadmin::templateAdminDetial($request->id);
        if ($temAdmin->users_id != $users->id) return OutputService::error("小程序备案管理员模板不存在!");
        return view('admin.template.admin_list_update',['temAdmin'=>$temAdmin]);
    }

    public function templateAdminUpdateSubmit(Request $request)
    {
        $users = Auth::user();
        $temAdmin = Templateadmin::templateAdminDetial($request->id);
        if ($temAdmin->users_id != $users->id) return OutputService::error("小程序备案管理员模板不存在!");
        Templateadmin::templateAdminUpdate($request->id,$request->manage_name,$request->manage_idcard,$request->manage_date_start,$request->manage_date_end,$request->manage_email,$request->manage_mobile,$request->manage_emergency_contact,$request->manage_photo_front,$request->manage_photo_back);
        return OutputService::success("小程序备案管理员模板更新成功!");
    }

    public function templatePerson(Request $request)
    {
        $users = Auth::user();
        $users_id = $users->id;
        $templateAdmins = Templateadmin::userTemplateAdmin($users_id);
        return OutputService::success("SUCCESS",$templateAdmins);
    }

    public function jstemplateMini(Request $request)
    {
        $users = Auth::user();
        $users_id = $users->id;
        $templateAdmins = Templatemini::userJsTemplateMini($users_id);
        return OutputService::success("SUCCESS",$templateAdmins);
    }
}